package com.hualan.servlet.goodsServlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.JsonResult;
import com.hualan.bean.PanghuShopGoods;
import com.hualan.enums.ResponseEnum;
import com.hualan.service.GoodsService;
import com.hualan.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet("/goods/querySearchShopGoodsList")
public class GoodsListServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GoodsService service = new GoodsServiceImpl();
		Enumeration<String> parameterNames = req.getParameterNames();
		HashMap<String, String> ps = new HashMap<>();
		while (parameterNames.hasMoreElements()) {
			String s = parameterNames.nextElement();
			String parameter = req.getParameter(s);
			if (parameter != null && !parameter.trim().equals("")){
				ps.put(s, parameter);
			}
		}
		Map<String, Object> goods = service.getGoods(ps);
		if (Objects.isNull(goods) || goods.isEmpty()){
			resp.getWriter().write(JSON.toJSONString(JsonResult.error(ResponseEnum.INCORRECT_PARAMS,"无此数据")));
		} else {
			resp.getWriter().write(JSON.toJSONString(JsonResult.success(goods)));
		}
	}
}
